package initiallize

import (
	"fmt"
	_ "github.com/mbobakov/grpc-consul-resolver"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"shop_webs/goods_web/global"
	"shop_webs/goods_web/proto"
	"shop_webs/goods_web/utils/otgrpc"
)

func InitSrvConn() {
	consulAddress := viper.GetString("consul.address")

	goodsConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s/%s?wait=14s&tag=srv", consulAddress, viper.GetString("consul.goodsSrv")),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
		grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())))
	if err != nil {
		zap.S().Fatal("[InitSrvConn] 连接 【商品服务失败】", "msg:", err.Error())
	}
	global.GoodsSrvClient = proto.NewGoodsClient(goodsConn)

}
